<?php $this->widget('application.extensions.infieldlabel.InFieldLabel'); ?>
<?php $this->widget('application.extensions.jqjson.JqueryJson'); ?>
<div class="form">
<br/><br/>
  <h2 class="gline" style="width: 170px;">רישום פעילות לימודיה</h2>
  <h5>לתאריך <?php echo date('d/m/Y',time()); ?></h5>
<br/>
<div>
<div style="float:right;width:150px;">
    <h4>    רשימת תלמידים שהוזנו</h4>
    <span id="results"></span></div>
<div style="float:right;">
<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'learning-form',
	'enableAjaxValidation'=>true,
)); ?>
<fieldset>
<div class="bline">
	<div class="row">
		<?php echo $form->label($model,'pupil_id'); ?>
		<?php  $this->widget('CAutoComplete',
                      array(
                            'name'=>'learning[pupil_id]',
                            'url'=>array('/learning/findPupilsByCenter'),
                            'max'=>10,
                            'minChars'=>1,
                            'delay'=>500,
                            'matchCase'=>false,
                            'methodChain'=>".result(function(event,item){\$(\"#learning_pupil\").val(item[1]);});",
                         ));
               ?>
		<?php //echo $form->error($model,'pupil_id'); ?>
	</div>

	<div class="row">
		<?php echo $form->label($model,'subject_id'); ?>
		<?php  $this->widget('CAutoComplete',
                      array(
                            'name'=>'learning[subject_id]',
                            'url'=>array('/learning/FindSubjectsByCenter'),
                            'max'=>10,
                            'minChars'=>1,
                            'delay'=>500,
                            'matchCase'=>false,
                            'methodChain'=>".result(function(event,item){\$(\"#learning_subject\").val(item[1]);});",
                         ));
                ?>
		<?php //echo $form->error($model,'subject_id'); ?>
	</div>
</div>
<div class="bline">
	<div class="row">
		<?php echo $form->labelEx($model,'recommendation'); ?>
		<?php echo $form->textField($model,'recommendation',array('size'=>60,'maxlength'=>256)); ?>
		<?php echo $form->error($model,'recommendation'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'helpsheets'); ?>
		<?php echo $form->textField($model,'helpsheets',array('size'=>60,'maxlength'=>256)); ?>
		<?php echo $form->error($model,'helpsheets'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'discipline'); ?>
		<?php echo $form->textField($model,'discipline',array('size'=>60,'maxlength'=>256)); ?>
		<?php echo $form->error($model,'discipline'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'notes'); ?>
		<?php echo $form->textArea($model,'notes',array('rows'=>6, 'cols'=>50)); ?>
		<?php echo $form->error($model,'notes'); ?>
	</div>
</div>
</fieldset>
        <?php echo CHtml::hiddenField('learning[subject]'); ?>
        <?php echo CHtml::hiddenField('learning[pupil]'); ?>
	<?php echo $form->errorSummary($model); ?>
	<div class="row buttons">
            <?php echo CHtml::button("הוסף תלמיד לרשימה", array('onclick'=>"javascript:$('form').submit();")); ?>
            <?php echo CHtml::button("שמור הכל", array('onclick'=>"javascript:saveForms();")); ?>
	</div>

<?php $this->endWidget(); ?>
</div>
</div><!-- form -->
<script language="javascript">
        /* Vars */
	$.savedForms = new Array();  // Forms array
	var $form = $("form:first"); // Define form
        var $sideList = $("#results");   // List element

        /* Methods */
        //Save form to list
        $form.submit( function () {
            //Get form data
            var fields = $(this).find(":input").serializeArray();
            //Encode form data to JSON and insert to array
            $.savedForms.push($.toJSON(fields));
            //alert($.toJSON(fields));
            updateSideList();
            ResetForm();
            return false; //block simple submit action
	});

	// Bind EnterKey to form submit
	$("form input, form select").live('keypress', function (e) {
		if ($(this).parents('form').find(
			'button[type=submit].default,input[type=submit].default').length <= 0)
			return true;
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
			$(this).parents('form').find('button[type=submit].default,input[type=submit].default').click();
			return false;
		} else
			return true;
	 }); // End EnterKey binding*/

         //Delete form from List
         function deleteForm(id){
             $.savedForms.splice(id,1); // Remove form savedForms array
             updateSideList();
         }

         //Edit form
         function editForm(id){
             alert('Edit form '+id);
         }


         //Update side list
         function updateSideList(){
             $sideList.empty(); //Clean list
            var html ="<ul class='sideList' dir='rtl'>";
            jQuery.each($.savedForms,function(i,savedForm){
                //Decode form data from JSON
                var formData = jQuery.parseJSON(savedForm);
                //Populate list
                html +='<li><a href="javascript:deleteForm('+i+');">X</a>';
                html +='<a href="javascript:editForm('+i+');">';
                html +=formData[0].value+'</a></li>';
                //html+='<li>'+formData[0].value+'</li>';
            }); //Forms loop
            html +="</ul>";
            $sideList.append(html);
         }
         function saveForms(){
             if(!$.savedForms.length){
                 alert('לא הוזן אף תלמיד');
                 return;
             }
             $('#message').html('שומר נתונים').addClass('saving').fadeIn();
             $.post('<?php echo Yii::app()->createUrl('/learning/createclass'); ?>',
                function(data) {
                    for (i=0 ; i<$.savedForms.length;i++)
                        $.post('<?php echo Yii::app()->createUrl('/learning/saveall/classId'); ?>/'+data,jQuery.parseJSON($.savedForms[i]),function(data){
                            $('#message').fadeOut();
                            window.location = "<?php echo Yii::app()->createUrl('/summaries/create');?>";
                        });
                 });



         }
/*Forms reset*/
        function ResetForm(){
        $(':input')
         .not(':button, :submit, :reset, :hidden')
         .val('')
         .removeAttr('checked')
         .removeAttr('selected');
         $(':input:first').focus();
         $('.errorMessage').hide();
         $('.row').removeClass('error').removeClass('success');
         $('.nicEdit-main').html('');
        }
$(document).ready(function() {
    ResetForm();
});
</script>